<template>
  <div class="app-container">
    <div class="menu-container" style="margin-bottom: 10px; float: right">
      <el-button
        v-waves
        type="primary"
        icon="el-icon-edit"
        @click="onClickCreateRole"
      >
        {{ $t("UserManager.Create") }}
      </el-button>

      <el-button
        v-waves
        type="danger"
        icon="el-icon-delete"
        @click="onClickDisableSome"
      >
        {{ $t("UserManager.Disable") }}
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @selection-change="handleCheckedRowChange"
    >
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column
        :label="$t('UserManager.id')"
        align="center"
        width="100px"
      >
        <template slot-scope="{ row }">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('UserManager.userName')"
        width="180px"
        align="center"
      >
        <template slot-scope="{ row }">
          <span> {{ row.userName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('UserManager.role')"
        width="180px"
        align="center"
      >
        <template slot-scope="{ row }">
          <span>{{ getRoleName(row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('UserManager.channel')"
        width="510px"
        align="center"
      >
        <template slot-scope="{ row }">
          <span>{{ getShowChannel(row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('UserManager.remarks')"
        width="380px"
        align="center"
      >
        <template slot-scope="{ row }">
          <span>{{ row.remarks }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('UserManager.operation')"
        align="center"
        width="280px"
      >
        <template slot-scope="{ row }">
          <el-button
            v-show="isDisableRow(row)"
            size="mini"
            type="primary"
            @click="onClickEdit(row)"
            >{{ $t("UserManager.edit") }}</el-button
          >
          <el-button
            v-show="isDisableRow(row)"
            size="mini"
            type="danger"
            @click="onClickDisable(row)"
            >{{ $t("UserManager.Disable") }}</el-button
          >
          <el-tag type="danger" v-show="!isDisableRow(row)"
            >{{ $t("UserManager.hadDisable") }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      center
      :title="getDialogTitle()"
      width="30%"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="tempShowData"
        style="width: 90%"
      >
        <el-form-item label="账号" label-width="100px">
          <el-input
            v-model="tempShowData.userName"
            type="text"
            placeholder="请输入账号"
            clearable
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="100px">
          <el-input
            v-model="tempShowData.pass"
            show-password
            type="text"
            placeholder="请输入密码"
            clearable
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="备注说明" label-width="100px">
          <el-input
            v-model="tempShowData.remarks"
            type="textarea"
            placeholder="请输入角色名称"
            clearable
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="渠道" label-width="100px">
          <el-checkbox
            :indeterminate="isIndeterminate"
            v-model="checkAllChannel"
            @change="handleCheckAllChange()"
            >全选</el-checkbox
          >
          <el-checkbox-group
            v-model="tempShowData.channelList"
            @change="handleCheckedOneChange()"
          >
            <el-checkbox
              v-for="data in channelConfigList"
              :label="data.id"
              :key="data.id"
              >{{ data.channename }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="用户权限" label-width="100px">
          <el-radio-group v-model="tempShowData.roleId">
            <el-radio
              style="margin-right: 10px; margin-bottom: 10px"
              v-for="role in roleList"
              :label="role.id"
              :key="role.id"
              >{{ role.roleName }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t("table.cancel") }}
        </el-button>
        <el-button type="primary" @click="onSureEdit()">
          {{ $t("table.confirm") }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Pagination from "@/components/Pagination/index.vue";
import { ChannelConfig } from "@/config/ChannelConfig";
import { PrivacyConfig } from "@/config/PrivacyConfig";
import { RoleModel } from "@/store/model/RoleModel";
import { UserModel } from "@/store/model/UserModel";
import { Logger } from "@/utils/Logger";
import { Form } from "element-ui";
import { Component, Vue } from "vue-property-decorator";
import SplitPane from "../components-demo/split-pane.vue";

@Component({
  name: "usermanager",
  components: {
    Pagination,
    SplitPane,
  },
})
export default class extends Vue {
  private tableKey = 0;
  private list: UserModel[] = [];
  private total = 0;
  private listLoading = true;

  private dialogFormVisible = false; //dialog是否显示

  private channelId: number = 0;

  private downloadLoading = false;

  //列表查询选项
  private listQuery = {
    page: 1, //第几页
    limit: 20, //一页多少个
    sort: "+id", //排序方式
  };

  private tempShowData: any = {};
  private rules = {};

  private DialogStateType = {
    Create: 0, //创建
    Edit: 1, //修改
  };

  private dialogState: number;

  private channelConfigList: Array<ChannelConfig> = [];
  private checkAllChannel: boolean = false;

  private checkedRowList: Array<UserModel> = [];
  private roleList: Array<RoleModel> = [];
  private isIndeterminate: boolean = false;

  mounted() {
    this.reInitDialogInfo();
    this.roleList = RoleModel.modelList;
    this.channelConfigList = ChannelConfig.getList(false);
    this.getList();
  }

  private isDisableRow(model: UserModel) {
    if (model.status == UserModel.UserStatus.Disable) {
      return false;
    } else {
      return true;
    }
  }

  private onClickDisableSome() {
    Logger.log("onClickDisableSome==", this.checkedRowList);
    for (let i = 0; i < this.checkedRowList.length; i++) {
      this.checkedRowList[i].status = UserModel.UserStatus.Disable;
    }
  }

  private handleCheckedRowChange(val) {
    this.checkedRowList = val;
    Logger.log("handleCheckedRowChange===", this.checkedRowList);
  }

  private reInitDialogInfo() {
    this.tempShowData = {};
    this.checkAllChannel = false
    this.isIndeterminate = false;
    this.tempShowData.channelList = []
  }

  private getShowChannel(model: UserModel) {
    let str: string = "";
    for (let i = 0; i < model.channelList.length; i++) {
      if (i != 0) {
        str += ",";
      }
      str = str + ChannelConfig.getChannelName(model.channelList[i]);
    }
    return str;
  }

  private getRoleName(model: UserModel) {
    let role: RoleModel = RoleModel.getRoleModel(model.roleId);
    if (role) {
      return role.roleName;
    } else {
      return "找不到角色id:" + model.roleId;
    }
  }

  private getSubGroupList(group: number) {
    let list: Array<PrivacyConfig> = PrivacyConfig.getSubGroupList(group);
    return list;
  }

  private handleCheckedOneChange() {
    Logger.log("handleCheckedOneChange===", this.tempShowData);
    if (this.tempShowData.channelList.length >= this.channelConfigList.length) {
      this.checkAllChannel = true;
      this.isIndeterminate = false;
    } else {
      this.isIndeterminate = true;
    }
    this.$forceUpdate(); //这里要强制刷新vue界面，防止界面刷新bug
  }

  private handleCheckAllChange() {
    if (this.checkAllChannel) {
      this.tempShowData.channelList = [];
      for (let i = 0; i < this.channelConfigList.length; i++) {
        this.tempShowData.channelList[i] = this.channelConfigList[i].id;
      }
    } else {
      this.tempShowData.channelList = [];
    }
  }

  private getDialogTitle() {
    if (this.dialogState == this.DialogStateType.Create) {
      return "新增用户";
    } else {
      return "编辑用户";
    }
  }

  private onClickCreateRole() {
    this.reInitDialogInfo();
    this.dialogFormVisible = true;
    this.dialogState = this.DialogStateType.Create;
    this.$nextTick(() => {
      (this.$refs.dataForm as Form).clearValidate(); // 移除所有表单项的校验
    });
  }

  private onClickEdit(model: UserModel) {
    this.reInitDialogInfo();
    this.tempShowData = Object.assign({}, model);
    this.dialogState = this.DialogStateType.Edit;
    this.dialogFormVisible = true;
    Logger.log("onClickEdit==", this.tempShowData);
    this.$nextTick(() => {
      (this.$refs.dataForm as Form).clearValidate(); // 移除所有表单项的校验
    });
  }

  private onClickDisable(model: UserModel) {
    model.status = UserModel.UserStatus.Disable;
  }

  private onSureEdit() {
    Logger.log("onSureEdit==", this.tempShowData);
    this.dialogFormVisible = false;
  }

  private async getList() {
    this.listLoading = true;
    UserModel.getShowList(this.listQuery, (result: Array<UserModel>) => {
      this.list = result;
      this.listLoading = false;
      this.total = UserModel.modelList.length;
    });
  }
}
</script>

<style lang="scss">
// .app-container {
//   .menu-container {
//     display: block;
//   }
// }
</style>